package cn.bupt.sse309.ydl.activity;

import cn.bupt.sse309.ydl.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ForgetPasswActivity extends Activity implements OnClickListener {

  private Button achieveBtn, nextBtn, backBtn;
  private EditText phoneNumberEt, verifyCodeEt;
  private TextView titleTv;

  public String phoneNumberStr;
  private int recLen = 60; // 获取验证码倒计时

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    this.setContentView(R.layout.activity_forget_passw);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.base_title_with_back);
    this.initView();
  }

  public void initView() {
    this.achieveBtn = (Button) this.findViewById(R.id.btn_achieve);
    this.nextBtn = (Button) this.findViewById(R.id.btn_next);
    this.phoneNumberEt = (EditText) this.findViewById(R.id.et_phonenumber);
    this.verifyCodeEt = (EditText) this.findViewById(R.id.et_verification_code);
    this.titleTv = (TextView) this.findViewById(R.id.baseTitleId);
    this.backBtn = (Button) this.findViewById(R.id.baseBack);
    this.titleTv.setText(R.string.title_forget_passw);
    this.achieveBtn.setOnClickListener(this);
    this.nextBtn.setOnClickListener(this);
    this.backBtn.setOnClickListener(this);

  }

  /*
   * (non-Javadoc)
   *
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_achieve:
      ForgetPasswActivity.this.phoneNumberStr = ForgetPasswActivity.this.phoneNumberEt.getText()
          .toString();
      if (ForgetPasswActivity.this.phoneNumberEt.length() <= 0) {
        Toast.makeText(this, "电话不能为空", 1).show();
        return;
      } else if (!ForgetPasswActivity.this.phoneNumberStr.matches("^1(3|5|7|8|4)\\d{9}")) {

        Toast.makeText(this, "电话号码有误！！！", 1).show();
        return;
      } else {
        this.phoneNumberStr = this.phoneNumberEt.getText().toString();
        Message message = this.handler.obtainMessage(1); // Message
        this.handler.sendMessageDelayed(message, 1000);
      }
      break;
    case R.id.btn_next:
      if (!TextUtils.isEmpty(this.verifyCodeEt.getText().toString())) {
        // SMSSDK.submitVerificationCode("86", this.phonenumber,
        // this.verificationCodeEt.getText().toString());
      } else {
        Toast.makeText(this, "验证码不能为空", 1).show();
      }

      break;

    case R.id.baseBack:
      ForgetPasswActivity.this.finish();
      break;
    default:
      break;
    }

  }

  Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      // TODO Auto-generated method stub

      switch (msg.what) {
      case 1:
        ForgetPasswActivity.this.recLen--;
        ForgetPasswActivity.this.achieveBtn.setText("重新获取"
            + ForgetPasswActivity.this.recLen + "秒");
        if (ForgetPasswActivity.this.recLen > 0) {
          Message message = ForgetPasswActivity.this.handler.obtainMessage(1);
          ForgetPasswActivity.this.handler.sendMessageDelayed(message, 1000); // send message
          ForgetPasswActivity.this.achieveBtn.setClickable(false);
          ForgetPasswActivity.this.achieveBtn.setBackgroundColor(0x545454);
        } else {
          ForgetPasswActivity.this.achieveBtn.setClickable(true);
        }
      }
      super.handleMessage(msg);
    }

  };
}
